/**
 * 健康指数计算工具
 * 提供健康指数相关的计算和评估功能
 */

import { 
  getTodayDrinkAmount, 
  getDrinkGoal, 
  getTodayMoodRecord, 
  getTodaySleepRecord, 
  getTodayToiletRecords 
} from './storage.js'

/**
 * 获取健康指数详情
 * @returns {Object} 健康指数详情对象
 */
export function getHealthIndexDetails() {
  // 获取各项指标数据
  const waterAmount = getTodayDrinkAmount()
  const waterGoal = getDrinkGoal()
  const moodRecord = getTodayMoodRecord()
  const sleepRecord = getTodaySleepRecord()
  const toiletRecords = getTodayToiletRecords()
  
  // 计算饮水指数
  const waterPercentage = Math.min(100, Math.round((waterAmount / waterGoal) * 100))
  const waterScore = calculateWaterScore(waterPercentage)
  
  // 计算情绪指数
  const moodScore = calculateMoodScore(moodRecord)
  
  // 计算睡眠指数
  const sleepScore = calculateSleepScore(sleepRecord)
  
  // 计算如厕指数
  const toiletScore = calculateToiletScore(toiletRecords)
  
  // 计算加权分数（每项25%权重）
  const waterWeight = 0.25
  const moodWeight = 0.25
  const sleepWeight = 0.25
  const toiletWeight = 0.25
  
  const waterWeightedScore = waterScore * waterWeight
  const moodWeightedScore = moodScore * moodWeight
  const sleepWeightedScore = sleepScore * sleepWeight
  const toiletWeightedScore = toiletScore * toiletWeight
  
  // 计算总分
  const totalScore = Math.round(
    waterWeightedScore + 
    moodWeightedScore + 
    sleepWeightedScore + 
    toiletWeightedScore
  )
  
  // 返回健康指数详情
  return {
    total: totalScore,
    water: {
      score: waterScore,
      weightedScore: waterWeightedScore,
      percentage: waterPercentage,
      amount: waterAmount,
      goal: waterGoal
    },
    mood: {
      score: moodScore,
      weightedScore: moodWeightedScore,
      hasRecord: !!moodRecord,
      type: moodRecord ? moodRecord.type : null
    },
    sleep: {
      score: sleepScore,
      weightedScore: sleepWeightedScore,
      hasRecord: !!sleepRecord,
      quality: sleepRecord ? sleepRecord.quality : null
    },
    toilet: {
      score: toiletScore,
      weightedScore: toiletWeightedScore,
      hasRecord: toiletRecords.length > 0,
      count: toiletRecords.length
    }
  }
}

/**
 * 计算饮水得分
 * @param {Number} percentage 饮水目标完成百分比
 * @returns {Number} 饮水得分
 */
function calculateWaterScore(percentage) {
  // 饮水得分与完成百分比成正比，最高100分
  return Math.min(100, percentage)
}

/**
 * 计算情绪得分
 * @param {Object|null} moodRecord 情绪记录
 * @returns {Number} 情绪得分
 */
function calculateMoodScore(moodRecord) {
  if (!moodRecord) {
    return 0 // 未记录情绪，不给予分数
  }
  
  // 根据情绪类型给予不同分数
  switch (moodRecord.type) {
    case 'happy':
      return 100 // 开心
    case 'excited':
      return 90 // 兴奋
    case 'neutral':
      return 80 // 一般
    case 'tired':
      return 70 // 疲惫
    case 'sad':
      return 60 // 低落
    default:
      return 70 // 其他情况
  }
}

/**
 * 计算睡眠得分
 * @param {Object|null} sleepRecord 睡眠记录
 * @returns {Number} 睡眠得分
 */
function calculateSleepScore(sleepRecord) {
  if (!sleepRecord) {
    return 0 // 未记录睡眠，不给予分数
  }
  
  // 根据睡眠质量给予不同分数
  switch (sleepRecord.quality) {
    case 'good':
      return 100 // 很好
    case 'normal':
      return 80 // 一般
    case 'bad':
      return 60 // 很差
    default:
      return 70 // 其他情况
  }
}

/**
 * 计算如厕得分
 * @param {Array} toiletRecords 如厕记录数组
 * @returns {Number} 如厕得分
 */
function calculateToiletScore(toiletRecords) {
  const count = toiletRecords.length
  
  if (count === 0) {
    return 0 // 未记录如厕，不给予分数
  } else if (count >= 1 && count <= 3) {
    return 100 // 1-3次，健康范围
  } else if (count === 4) {
    return 90 // 4次，稍多
  } else if (count >= 5 && count <= 6) {
    return 80 // 5-6次，偏多
  } else {
    return 70 // 7次及以上，过多
  }
}

/**
 * 获取健康评估
 * @param {Number} score 健康指数总分
 * @returns {Object} 健康评估对象
 */
export function getHealthAssessment(score) {
  let rating, suggestion
  
  if (score >= 95) {
    rating = '完美'
    suggestion = '太棒了！你完成了所有健康任务，继续保持！'
  } else if (score >= 75) {
    rating = '优秀'
    suggestion = '你的表现非常好，继续坚持记录健康数据。'
  } else if (score >= 50) {
    rating = '良好'
    suggestion = '完成了部分健康记录，记得及时补充其他记录哦。'
  } else if (score >= 25) {
    rating = '一般'
    suggestion = '开始记录健康数据了，继续加油！'
  } else {
    rating = '未记录'
    suggestion = '开始记录今天的健康数据吧！'
  }
  
  return { rating, suggestion }
}

/**
 * 获取健康指数进度数据
 * @returns {Array} 健康指数进度数据数组
 */
export function getHealthIndexProgressData() {
  const healthIndex = getHealthIndexDetails()
  
  return [
    {
      label: '饮水',
      value: healthIndex.water.score,
      color: '#4CAF50',
      weight: 0.3
    },
    {
      label: '情绪',
      value: healthIndex.mood.score,
      color: '#FF9800',
      weight: 0.25
    },
    {
      label: '睡眠',
      value: healthIndex.sleep.score,
      color: '#2196F3',
      weight: 0.25
    },
    {
      label: '如厕',
      value: healthIndex.toilet.score,
      color: '#9C27B0',
      weight: 0.2
    }
  ]
}